package qeug.shsh.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * 付款申请单撤销插件：撤销后将对应的银行存款，承兑汇票，信用证金额字段返回资金计划中的金额
 */
public class PaymentAdjustmentUnsubOp extends AbstractOperationServicePlugIn {
    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
        DynamicObject[] dataEntities = e.getDataEntities();
        for (DynamicObject dataEntity : dataEntities) {
            Long id = dataEntity.getLong("id");
            DynamicObject date = BusinessDataServiceHelper.loadSingle("er_publicreimbursebill", new QFilter[]{
                    new QFilter("id", QCP.equals, id)});//对公报销单
            boolean aBoolean = date.getBoolean("qeug_budgettype");//是否引用资金计划
            if (aBoolean) {
                BigDecimal actuallyPaidAmount = date.getBigDecimal("qeug_amount");//实付金额
                String fundPlAnEntry = date.getString("qeug_fundplanentry");//资金计划分录内码
                String planType = date.getString("qeug_plantype");//计划类型
                if (fundPlAnEntry != null) {
                    long fundPlAnEntryLong = Long.parseLong(fundPlAnEntry);
                    QFilter[] qFilter = new QFilter[]{
                            new QFilter("entryentity.id", QCP.equals, fundPlAnEntryLong)};
                    List<DynamicObject> orderList = new ArrayList<>();
                    DynamicObject qeug_fundplan = BusinessDataServiceHelper.loadSingle("qeug_fundplan", qFilter);//资金计划
                    DynamicObjectCollection entryCollection = qeug_fundplan.getDynamicObjectCollection("entryentity");//资金计划分录
                    for (DynamicObject expense : entryCollection) {
                        if (fundPlAnEntry.equals(expense.get("id").toString())) {
                            BigDecimal difference = BigDecimal.ZERO;
                            if ("0".equals(planType)) {
                                BigDecimal qeug_newbankamount = expense.getBigDecimal("qeug_bankamount");//银行存款
                                BigDecimal qeug_amountfield2 = expense.getBigDecimal("qeug_amountfield2");//累计扣减银行存款
                                difference = qeug_newbankamount.add(actuallyPaidAmount).setScale(5, RoundingMode.HALF_UP);//银行存款减去实付金额
                                BigDecimal amountfield2 = qeug_amountfield2.subtract(actuallyPaidAmount);
                                expense.set("qeug_amountfield2", amountfield2);//累计扣减银行存款
                                expense.set("qeug_bankamount", difference);//剩余银行存款
                            } else if ("1".equals(planType)) {
                                BigDecimal qeug_newyshpamount = expense.getBigDecimal("qeug_hpamount");//承兑汇票
                                BigDecimal qeug_amountfield1 = expense.getBigDecimal("qeug_amountfield1");//累计扣减承兑汇票
                                difference = qeug_newyshpamount.add(actuallyPaidAmount).setScale(5, RoundingMode.HALF_UP);//承兑汇票减去实付金额
                                BigDecimal amountfield1 = qeug_amountfield1.subtract(actuallyPaidAmount);
                                expense.set("qeug_amountfield1", amountfield1);//累计扣减承兑汇票
                                expense.set("qeug_hpamount", difference);//剩余承兑汇票
                            } else if ("2".equals(planType)) {
                                BigDecimal qeug_newxyzamount = expense.getBigDecimal("qeug_xyzamount");//信用证
                                BigDecimal qeug_amountfield3 = expense.getBigDecimal("qeug_amountfield3");//累计扣减信用证
                                difference = qeug_newxyzamount.add(actuallyPaidAmount).setScale(5, RoundingMode.HALF_UP);//信用证减去实付金额
                                BigDecimal amountfield3 = qeug_amountfield3.subtract(actuallyPaidAmount);
                                expense.set("qeug_amountfield3", amountfield3);//累计扣减信用证
                                expense.set("qeug_xyzamount", difference);//剩余信用证
                            }
                            orderList.add(qeug_fundplan);
                            SaveServiceHelper.save(orderList.toArray(new DynamicObject[0]));
                        }
                    }
                }
            }
        }
    }
}
